package com.jintian.smart.platform.system.mapper;

import com.jintian.smart.kernel.orm.mapper.IBaseExtMapper;
import com.jintian.smart.base.api.system.entity.HelpEntity;
import com.jintian.smart.base.api.system.form.HelpQueryForm;
import com.jintian.smart.base.api.system.form.HelpRelationForm;
import com.jintian.smart.base.api.system.form.HelpViewRecordQueryForm;
import com.jintian.smart.base.api.system.view.HelpRelationVO;
import com.jintian.smart.base.api.system.view.HelpVO;
import com.jintian.smart.base.api.system.view.HelpViewRecordVO;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.mapper.annotation.Mapper;
import org.beetl.sql.mapper.annotation.Param;
import org.beetl.sql.mapper.annotation.SqlResource;
import org.beetl.sql.mapper.annotation.Update;

import java.util.List;

/**
 * 帮助文档 dao
 */
@Mapper
@SqlResource("system.Help")
public interface HelpMapper extends IBaseExtMapper<HelpEntity> {
    // ================================= 帮助文档【主表 sys_help 】   =================================

    /**
     * 查询 全部相关文档
     *
     * @return
     */
    List<HelpVO> queryAllHelpDocList();

    /**
     * 后管分页查询帮助文档
     *
     * @param queryForm
     * @return
     */
    PageResult<HelpVO> query(HelpQueryForm queryForm);


    /**
     * 更新 阅读量
     *
     * @param helpDocId
     * @param userViewCountIncrease
     * @param pageViewCountIncrease
     */
    @Update
    void updateViewCount(@Param("helpDocId") Long helpDocId, @Param("userViewCountIncrease") Integer userViewCountIncrease, @Param("pageViewCountIncrease") Integer pageViewCountIncrease);


    /**
     * 根据目录，查询文档
     *
     * @param helpDocCatalogId
     * @return
     */
    List<HelpVO> queryHelpDocByCatalogId(@Param("helpDocCatalogId") Long helpDocCatalogId);

    /**
     * 根据关联文档id，查询文档
     *
     * @param relationId
     * @return
     */
    List<HelpVO> queryHelpDocByRelationId(@Param("relationId") Long relationId);

    // ================================= 关联项目 【子表 t_help_doc_relation 】 =================================

    /**
     * 保存 关联
     *
     * @param helpDocId
     * @param relationList
     */
    @Update
    void insertRelation(@Param("helpDocId") Long helpDocId, @Param("relationList") List<HelpRelationForm> relationList);

    /**
     * 删除关联
     *
     * @param helpDocId
     */
    @Update
    void deleteRelation(@Param("helpDocId") Long helpDocId);

    /**
     * 查询关联
     *
     * @param helpDocId
     */
    List<HelpRelationVO> queryRelationByHelpDoc(@Param("helpDocId") Long helpDocId);

    // ================================= 查看记录【子表 sys_help_view_record】   =================================

    /**
     * 查询某个用户的指定文档的阅读量
     *
     * @param helpDocId
     * @param userId
     * @return
     */
    long viewRecordCount(@Param("helpDocId") Long helpDocId, @Param("userId") Long userId);

    /**
     * 查询帮助文档的 查看记录
     *
     * @param queryForm
     * @return
     */
    PageResult<HelpViewRecordVO> queryViewRecordPage(@Param("queryForm") HelpViewRecordQueryForm queryForm);

    /**
     * 保存查看记录
     *
     * @param helpDocId
     * @param userId
     * @param userName
     * @param ip
     * @param userAgent
     */
    @Update
    void insertViewRecord(@Param("helpDocId") Long helpDocId, @Param("userId") Long userId, @Param("userName") String userName, @Param("ip") String ip, @Param("userAgent") String userAgent, @Param("pageViewCount") Integer pageViewCount);

    /**
     * 更新查看记录
     *
     * @param helpDocId
     * @param userId
     * @param ip
     * @param userAgent
     */
    @Update
    void updateViewRecord(@Param("helpDocId") Long helpDocId, @Param("userId") Long userId, @Param("ip") String ip, @Param("userAgent") String userAgent);
}
